<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/2/16
 * Time: 9:57
 */

namespace Admin\Controller;


class FuturesController extends AdminController
{
	/**
	* 期货合约列表
	* 
	* @return
	*/
    public function index(){
    	$model   =   D('futures');
    	if($_REQUEST['status']){//开启状态
            if($_REQUEST['status']==1){
                $map['status']=array('eq',1);
                $status=1;
            }elseif($_REQUEST['status']==2){
                $map['status']=array('eq',0);
                $status=2;
            }else{
                $status=-1;
            }
        }
        else{
            $status=-1;
        }
       	$this->assign('status',$status);
       	if($_REQUEST['name']){//名称
            $map['name']=array('like',"%".trim($_REQUEST['name'])."%");
        }
        if($_REQUEST['code']){//代码
            $map['code']=array('like',"%".trim($_REQUEST['code'])."%");
        }
        if ($_REQUEST['create_time']) {
            $time=strtotime($_REQUEST['create_time']);
            $start_time=mktime(00,00,00,date('m',$time),date('d',$time),date('Y',$time));
            $end_time=mktime(23,59,59,date('m',$time),date('d',$time),date('Y',$time));
            $map['create_time'] = array('between',"$start_time,$end_time");
        }
        $list = $this->lists($model,$map,'id desc');
        $this->assign('list',$list);
        $this->display();
    }
    /**
	* 期货合约添加
	*/
	public function add(){
		if(IS_POST){
			$name=I('post.name');
			if(empty($name)){
				$this->error('期货名称不能为空');
			}
			$code=I('post.code');
			if(empty($code)){
				$this->error('期货代码不能为空');
			}
			$total=I('post.total');
			if(!is_numeric($total)){
				$this->error('期货总量为整数');
			}
			$maker=I('post.maker');
			if(empty($maker)){
				$this->error('做市商不能为空');
			}
			$price=I('post.price');
			if(!is_numeric($price)){
				$this->error('价格位数字');
			}
		
			$premium=I('post.premium');
			
			if(!is_numeric($premium)){
				$this->error('权利金为数字');
			}
			$stop_loss=I('stop_loss');
			$authority_status=I('post.authority_status');
			$type_sign=I('post.type_sign');
			if(empty($type_sign)){
				$this->error('请选择期货类型标识');
				exit;
			}
			$begin_time=I('post.begin_time');
			if(empty($begin_time)){
				$this->error('开始时间不能为空');
				exit;
			}
			$end_time=I('post.end_time');
			if(empty($end_time)){
				$this->error('结束时间不能为空');
				exit;
			}
			$data['status']=I('status');
			$data['name']=$name;
			$data['code']=$code;
			$data['total']=$total;
			$data['maker']=$maker;
			$data['type_sign']=I('post.type_sign');
			$data['price']=doubleval($price);
			$data['premium']=doubleval($premium);
			$data['stop_loss']=$stop_loss;
			$data['authority_status']=$authority_status;
			$data['adm_id']=session('user_auth.uid');
			$data['begin_time']=strtotime(I('post.begin_time'));
			$data['end_time']=strtotime(I('post.end_time'));
			$data['create_time']=time();
            if(M('futures')->add($data)){
                $this->success('新增成功',U('Futures/index'));
            }else{
                $this->error('新增失败');
            }
          
        }
		$this->display();
	}
	/**
	* 编辑期货合约数据
	* 
	* @return
	*/
	public function edit(){
		if(IS_POST){
			$edit_where['id']=I('post.id');
            $name=I('post.name');
            if(empty($name)){
                $this->error('期货名称不能为空');
            }
            $code=I('post.code');
            if(empty($code)){
                $this->error('期货代码不能为空');
            }

			$total=I('post.total');
			if(empty($total)){
				$this->error("期货总数不能为空");
			}
			$begin_time=I('post.begin_time');
			if(empty($begin_time)){
				$this->error('开始时间不能为空');
			}
			$end_time=I('post.end_time');
			if(empty($end_time)){
				$this->error('结束时间不能为空');
			}
			$total=I('post.total');
			if(!is_numeric($total)){
				$this->error('期货总量为整数');
			}
			$maker=I('post.maker');
			if(empty($maker)){
				$this->error('做市商不能为空');
			}
			$price=I('post.price');
			if(!is_numeric($price)){
				$this->error('价格位数字');
			}
			
			$premium=I('post.premium');
			
			if(!is_numeric($premium)){
				$this->error('权利金为数字');
			}

			$stop_loss=I('stop_loss');
			$authority_status=I('post.authority_status');
			$type_sign=I('post.type_sign');
			if(empty($type_sign)){
				$this->error('期货类型标识不能为空');
			}
			$filename="yixi.txt";
			$result=M('futures')->where($edit_where)->find();
			if($price!=$result['price'] || $premium!=$result['premium']){
					$lastArr=unserialize(file_get_contents($filename));
					if($price!=$result['price']){
						$lastArr[$edit_where['id']]['price']=$result['price'];
					}
					if($premium!=$result['premium']){
						$lastArr[$edit_where['id']]['premium']=$result['premium'];
					}
				file_put_contents($filename,serialize($lastArr));
			}
            $data['name']=$name;
            $data['code']=$code;
			$data['type_sign']=$type_sign;
			$data['price']=doubleval($price);
			$data['premium']=doubleval($premium);
			$data['stop_loss']=$stop_loss;
			$data['authority_status']=$authority_status;
			$data['status']=I('post.status');
			$data['total']=I('post.total');
			$data['begin_time']=strtotime($begin_time);
			$data['end_time']=strtotime($end_time);
			if(M('futures')->where($edit_where)->save($data)===false){
				$this->error('编辑失败');
			}else{
				$this->success('编辑成功',U('Futures/index'));
			}
			
		}
		$id=I('get.id');
		$where['id']=$id;
		$list=M('futures')->where($where)->find();
		$lastArr=unserialize(file_get_contents("yixi.txt"));
		$list['lastprice']=$lastArr[$id]['price'];
		$list['lastpremium']=$lastArr[$id]['premium'];
		$this->assign('list',$list);
		$this->display();
	}
	/**
	* 删除期货合约数据
	*/
	public function delete(){
		$ids    =   I('request.ids');
        $m=M('futures');
        if(is_array($ids)){
            $where = 'id in('.implode(',',$ids).')';
        }else{
            $where=array('id'=>$ids);
        }
        if($m->where($where)->delete()){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
	}

}